import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mall/constant/app_colors.dart';
import 'package:mall/gen/r.dart';
import 'package:mall/modules/model/goods_detail_entity.dart';
import 'package:mall/utils/image_component/image_component.dart';
import 'package:mall/utils/image_component/image_preview_dialog.dart';

class GoodsDetailCommentWidget extends StatelessWidget {
  const GoodsDetailCommentWidget({super.key, required this.model});
  final GoodsDetailComment? model;
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 15, right: 15, top: 15),
      child: Column(
        children: [
          //评论
          Row(
            children: [
              Text("评价", style: TextStyle(fontSize: 15, color: AppColors.C_222222)),
              SizedBox(width: 10),
              //条数
              Text("${model?.count ?? 0}条", style: TextStyle(fontSize: 13, color: AppColors.C_C0C0C0)),
              Spacer(),
              ImageComponent(R.image.goTo.path, width: 6, height: 10),
            ],
          ),

          if (model?.data != null && model!.data!.length > 0)
            ListView.builder(
              padding: EdgeInsets.only(top: 10),
              itemCount: model?.data?.length ?? 0,
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemBuilder: (BuildContext context, int index) {
                final data = model!.data![index];
                return Container(
                  padding: EdgeInsets.only(bottom: 10),
                  child: _contont(data),
                );
              },
            ),
        ],
      ),
    );
  }

  Column _contont(GoodsDetailCommentModel data) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            //头像
            ImageComponent(
              data.avatar ?? "",
              width: 30,
              height: 30,
              borderRadius: BorderRadius.circular(15),
            ),
            SizedBox(width: 10),
            //昵称
            Text(data.nickname ?? "", style: TextStyle(fontSize: 12, color: AppColors.C_999999)),
          ],
        ),
        SizedBox(height: 10),
        //内容
        Text(
          data.content ?? "",
          style: TextStyle(
            fontSize: 14,
            color: AppColors.C_222222,
          ),
          maxLines: 3,
          overflow: TextOverflow.ellipsis,
        ),
        SizedBox(height: 10),
        if (data.picList != null && data.picList!.length > 0)
          GridView.builder(
              padding: EdgeInsets.zero,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 4,
                mainAxisSpacing: 10,
                childAspectRatio: 1.0,
              ),
              itemCount: data.picList!.length > 3 ? 3 : data.picList!.length,
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemBuilder: (BuildContext context, int index) {
                return InkWell(
                  child: Container(
                    child: ImageComponent(
                      data.picList?[index] ?? "",
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  onTap: () {
                    Get.dialog(ImagePreviewDialog(imgList: data.picList!, index: index));
                  },
                );
              }),
      ],
    );
  }
}
